<?php

namespace Core\annotationhandlers;

use Core\annotations\Bean;
use Core\annotations\Value;
use DI\Container;

return [
    //类注解
    Bean::class => function ($instance, Container $container, Bean $bean) {
        //获取类属性值
        $vars = get_object_vars($bean);
        if (isset($vars['name']) && $vars['name']) {
            //通过注解名称
            $beanName = $vars['name'];
        } else {
            //获取短名称
            //获取实例名称
            $className = get_class($instance);
            //['app','controllers','UserController']
            $arrs = explode("\\", $className);
            //获取短名称
            $beanName = end($arrs);
        }
        //短名称 注入容器
        $container->set($beanName, $instance);
    },

    //属性注解
    Value::class => function (\ReflectionProperty $property, $instance, Value $value) {
        $ini = parse_ini_file(ROOT_PATH . "/env",true);
        $vars = get_object_vars($value);
        if (!$vars) {
            return $instance;
        }
        foreach ($vars as $var) {
            if ($var == "") {
                continue;
            }
            if (isset($ini[$var])) {
                $property->setValue($instance, $ini[$var]);
            }
        }
        return $instance;
    }
];
